package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.DestinationCommendTheme;
import cn.wolfcode.luowowo.article.mapper.DestinationCommendThemeMapper;
import cn.wolfcode.luowowo.article.query.DestinationCommendThemeQuery;
import cn.wolfcode.luowowo.article.service.IDestinationCommendThemeService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class DestinationCommendThemeServiceImpl implements IDestinationCommendThemeService {

    @Autowired
    private DestinationCommendThemeMapper themeMapper;

    @Override
    public PageInfo query(DestinationCommendThemeQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(themeMapper.query(qo));
    }

    @Override
    public void saveOrUpdate(DestinationCommendTheme commendTheme) {

        if (commendTheme.getSequence() == null) {
            int max = themeMapper.selectMaxSequence();
            commendTheme.setSequence(max + 1);
        }

        if (commendTheme.getId() != null) {
            themeMapper.updateByPrimaryKey(commendTheme);
        }else {
            themeMapper.insert(commendTheme);
        }
    }

    @Override
    public void delete(Long id) {
        themeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<DestinationCommendTheme> getThemeByType(int type) {
        return themeMapper.selectThemeByType(type);
    }
}
